package com.linkai.controller.admin.info;

import com.linkai.controller.admin.BaseController;
import com.linkai.entity.Bstore;
import com.linkai.enums.ResultDTO;
import com.linkai.service.BstoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @Author yamon
 * @Date 2021-01-05 20:15
 * @Description
 * @Version 1.0
 */
@Controller
@RequestMapping("/admin/info/store")
public class StoreController extends BaseController {
    @Autowired
    private BstoreService bstoreService;
    /**
     * 首页
     * @return index
     */
    @GetMapping("/index")
    public ModelAndView index(){
        final ModelAndView modelAndView = new ModelAndView();
        final List<Bstore> list = bstoreService.list();
        modelAndView.addObject("stores",list);
        modelAndView.setViewName("admin/info/store/index");
        return modelAndView;
    }

    /**
     * 跳转增加页面
     * @return list
     */
    @GetMapping("/add")
    public String add(){
        return "admin/info/store/add";
    }

    /**
     * 跳转到页面，修改页面并获取数据
     * @param id id
     * @return page
     */
    @GetMapping("/add/{id}")
    public ModelAndView add(@PathVariable Integer id){
        final ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("store",bstoreService.getById(id));
        modelAndView.setViewName("admin/info/store/add");
        return modelAndView;
    }
    /**
     * 保存或者修改一个职员信息
     * @return BOOLEAN
     */
    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public ResultDTO<Boolean> addEmployee(@RequestBody Bstore bstore){
        if (bstore.getBstoId()!=null){
            //更新
            return super.updateInfo(bstoreService.updateById(bstore));
        }
        return super.successSaveInfo(bstoreService.save(bstore));
    }

    /**
     * 根据id得到实体类
     * @param id id
     * @return 实体类
     */
    @GetMapping("/get/{id}")
    public Bstore getOneEmployee(@PathVariable Integer id){
        return bstoreService.getById(id);
    }



    /**
     * 根据id删除所有职员信息
     * @param id id
     * @return flag
     */
    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public ResultDTO<Boolean> deleteOneEmployee(@PathVariable Integer id){
        return super.successDeleteInfo(bstoreService.removeById(id));
    }
}
